import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/Login.vue";
import Home from "@/views/Home.vue";
// 导入store
import store from '@/store';
import { Message, Loading } from 'element-ui';

Vue.use(VueRouter);
const router = new VueRouter({
    mode: 'hash',
    routes: [{
        path: '/register',
        name: 'register',
        component: () => import("@/views/Register.vue")
    }, {
        path: '/login',
        name: 'login',
        component: Login
    }, {
        path: '/',
        // name: 'home',
        component: Home,
        children: [{
            path: '',
            redirect: 'index'
        }, {
            path: 'index',
            name: 'index',
            meta: {
                title: "首页",
            },
            component: () => import("@/views/Home/Index.vue"),

        }, {
            path: 'iot',
            // name: 'iot',
            meta: {
                title: "物联网",
            },
            component: () => import("@/views/Home/Iot.vue"),
            children: [{
                path: '',
                redirect: 'template',
            }, {
                path: 'template',
                name: "template",
                meta: {
                    title: '通用物模型',
                },
                component: () => import("@/views/Home/Iot/Template.vue")
            }, {
                path: 'category',
                name: "category",
                meta: {
                    title: '产品分类',
                },
                component: () => import("@/views/Home/Iot/Category.vue")
            }, {
                path: 'product',
                name: "product",
                meta: {
                    title: '产品管理',
                },
                component: () => import("@/views/Home/Iot/Product.vue")
            }, {
                path: 'firmware',
                name: "firmware",
                meta: {
                    title: '产品固件',
                },
                component: () => import("@/views/Home/Iot/Firmware.vue")
            }, {
                path: 'group',
                name: "group",
                meta: {
                    title: '设备分组',
                },
                component: () => import("@/views/Home/Iot/Group.vue")
            }, {
                path: 'device',
                name: "device",
                meta: {
                    title: '设备管理',
                },
                component: () => import("@/views/Home/Iot/Device.vue")
            }, {
                path: 'alertLog',
                name: "alertLog",
                meta: {
                    title: '设备告警',
                },
                component: () => import("@/views/Home/Iot/AlertLog.vue")
            }, {
                path: 'clientDetails',
                name: "clientDetails",
                meta: {
                    title: '云云对接',
                },
                component: () => import("@/views/Home/Iot/ClientDetails.vue")
            }]

        }, {
            path: 'emqx',
            // name: 'emqx',
            meta: {
                title: "EMQ管理",
            },
            component: () => import("@/views/Home/Emqx.vue"),
            children: [{
                path: '',
                redirect: 'client',
            }, {
                path: 'client',
                name: "client",
                meta: {
                    title: '客户端',
                },
                component: () => import("@/views/Home/Emqx/Client.vue")
            }, {
                path: 'listener',
                name: "listener",
                meta: {
                    title: '监听器',
                },
                component: () => import("@/views/Home/Emqx/Listener.vue")
            }, {
                path: 'topic',
                name: "topic",
                meta: {
                    title: '消息主题',
                },
                component: () => import("@/views/Home/Emqx/Topic.vue")
            }, {
                path: 'subscribe',
                name: "subscribe",
                meta: {
                    title: '消息订阅',
                },
                component: () => import("@/views/Home/Emqx/Subscribe.vue")
            }, {
                path: 'plugin',
                name: "plugin",
                meta: {
                    title: '插件管理',
                },
                component: () => import("@/views/Home/Emqx/Plugin.vue")
            }, {
                path: 'resource',
                name: "resource",
                meta: {
                    title: '规则资源',
                },
                component: () => import("@/views/Home/Emqx/Resource.vue")
            }, {
                path: 'rule',
                name: "rule",
                meta: {
                    title: '规则引擎',
                },
                component: () => import("@/views/Home/Emqx/Rule.vue")
            }]

        }, {
            path: 'system',
            // name: 'system',
            meta: {
                title: "系统管理",
            },
            component: () => import("@/views/Home/System.vue"),
            children: [{
                path: '',
                redirect: 'user',
            }, {
                path: 'user',
                name: "user",
                meta: {
                    title: '用户管理',
                },
                component: () => import("@/views/Home/System/User.vue")
            }, {
                path: 'role',
                name: "role",
                meta: {
                    title: '角色管理',
                },
                component: () => import("@/views/Home/System/Role.vue")
            }, {
                path: 'menu',
                name: "menu",
                meta: {
                    title: '菜单管理',
                },
                component: () => import("@/views/Home/System/Menu.vue")
            }, {
                path: 'dept',
                name: "dept",
                meta: {
                    title: '部门管理',
                },
                component: () => import("@/views/Home/System/Dept.vue")
            }, {
                path: 'post',
                name: "post",
                meta: {
                    title: '岗位管理',
                },
                component: () => import("@/views/Home/System/Post.vue")
            }, {
                path: 'dict',
                name: "dict",
                meta: {
                    title: '字典管理',
                },
                component: () => import("@/views/Home/System/Dict.vue")
            }, {
                path: 'config',
                name: "config",
                meta: {
                    title: '参数设置',
                },
                component: () => import("@/views/Home/System/Config.vue")
            }, {
                path: 'notice',
                name: "notice",
                meta: {
                    title: '通知公告',
                },
                component: () => import("@/views/Home/System/Notice.vue")
            }, {
                path: 'log',
                // name: "log",
                meta: {
                    title: '日志管理',
                },
                component: () => import("@/views/Home/System/Log.vue"),
                children: [{
                    path: '',
                    redirect: 'operlog'
                }, {
                    path: 'operlog',
                    name: 'operlog',
                    meta: {
                        title: '操作日志'
                    },
                    component: () => import("@/views/Home/System/Log/Operlog.vue"),
                }, {
                    path: 'logininfor',
                    name: 'logininfor',
                    meta: {
                        title: '登录日志'
                    },
                    component: () => import("@/views/Home/System/Log/Logininfor.vue"),
                }]
            }, {
                path: 'newsCategory',
                name: "newsCategory",
                meta: {
                    title: '新闻分类',
                },
                component: () => import("@/views/Home/System/NewsCategory.vue")
            }, {
                path: 'news',
                name: "news",
                meta: {
                    title: '新闻咨询',
                },
                component: () => import("@/views/Home/System/News.vue")
            }, {
                path: 'platform',
                name: "platform",
                meta: {
                    title: '三方登录',
                },
                component: () => import("@/views/Home/System/Platform.vue")
            },]

        },
        {
            path: 'monitor',
            // name: 'monitor',
            meta: {
                title: "系统监控",
            },
            component: () => import("@/views/Home/Monitor.vue"),
            children: [{
                path: '',
                redirect: "online"
            }, {
                path: 'online',
                name: 'online',
                meta: {
                    title: '在线用户'
                },
                component: () => import('@/views/Home/Monitor/Online.vue')
            }, {
                path: 'job',
                name: 'job',
                meta: {
                    title: '定时任务'
                },
                component: () => import('@/views/Home/Monitor/Job.vue')
            }, {
                path: 'druid',
                name: 'druid',
                meta: {
                    title: '数据监控'
                },
                component: () => import('@/views/Home/Monitor/Druid.vue')
            }, {
                path: 'server',
                name: 'server',
                meta: {
                    title: '服务监控'
                },
                component: () => import('@/views/Home/Monitor/Server.vue')
            }, {
                path: 'cache',
                name: 'cache',
                meta: {
                    title: '缓存监控'
                },
                component: () => import('@/views/Home/Monitor/Cache.vue')
            }]

        }, {
            path: 'tool',
            // name: 'tool',
            meta: {
                title: "系统工具",
            },
            component: () => import("@/views/Home/Tool.vue"),
            children: [{
                path: '',
                redirect: 'build'
            }, {
                path: 'build',
                name: 'build',
                meta: {
                    title: '表单构建'
                },
                component: () => import('@/views/Home/Tool/Build.vue')
            }, {
                path: 'gen',
                name: 'gen',
                meta: {
                    title: '代码生成'
                },
                component: () => import('@/views/Home/Tool/Gen.vue')
            }, {
                path: 'swagger',
                name: 'swagger',
                meta: {
                    title: '系统接口'
                },
                component: () => import('@/views/Home/Tool/Swagger.vue')
            }]

        }]
    }, {
        path: '*',
        name: 'errorpage',
        component: () => import('@/views/ErrorPage.vue')
    }]
});
/* 全局前置守卫 */
let LoadingIns;
router.beforeEach(async (to, from, next) => {
    let user = store.state.user;
    if (to.path !== '/login' && to.meta.title !== '404' && to.path !== "/register") {
        LoadingIns = Loading.service({
            background: 'rgba(0,0,0,0.7)',
            text: '加载中...'
        });
        let token = localStorage.getItem("token");
        user = await store.dispatch('setUserAsync');
        if (!token) {
            // 当前用户是没有登录的：跳转到登录也，并且传递目标地址
            Message.error('您还未登录，请您先登录！')
            next(`/login?to=${to.path}`);
            return;
        }
        // console.log(user, 'store');
        /*  let permissions = user.permissions,
            userPermissions = to.path;
        console.log(userPermissions, 'router');
        if (userPermissions) {
            userPermissions = to.path.replace(/\//g, ":")
            if (!permissions.includes(userPermissions)) {
                Message.error('您无权限访问，请联系管理员')
                next(from.fullPath);
                return
            }
        }
*/

    }
    next()
})


/* 全局后置守卫 */
router.afterEach(to => {
    /*  let { title } = to.meta;
     document.title = title ? `新峰CMS系统-${title}` : '新峰CMS系统' */
    // 关闭loading
    if (LoadingIns) LoadingIns.close();
})

export default router;